<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>地图导航</title>
    <style>
        *{
         margin: 0;
         padding: 0;
        }
        #container{
         width: 100%;
         height: 100vh;
         background-color: white;
        }
        .custom-content-marker{
            position: relative;
            width: 25px;
            height: 34px;

        }
        .custom-content-marker img{
            width: 100%;
            height: 100%;
        }
        .custom-content-marker .close-btn{
            position: absolute;
            top: -6px;
            right: -8px;
            width: 15px;
            height: 15px;
            font-size: 12px;
            background-color: #ccc;
            border-radius: 50%;
            color: #fff;
            text-align: center;
            line-height: 15px;
            box-shadow: -1px 1px 1px rgba(10,10,10,0.2);
        }
        .custom-content-marker .close-btn:hover{
            background-color: #666;
        }
    </style>
</head>
<body>
<div id="container"></div>
</body>
</html>

<script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: "ae9d677b396af9bdfcd06d8bc9cfae30",
    };
  </script>
  <script
    type="text/javascript"
    src="https://webapi.amap.com/maps?v=2.0&key=682579e48126d3a320266b003b02e995"
  ></script>
  <script type="text/javascript">
    //地图初始化应该在地图容器 <div> 已经添加到 DOM 树之后
    var map = new AMap.Map("container", {
      zoom: 18, //地图级别
      center: [118.7305, 32.1422], //初始地图中心点
    });

    window.onload=function(){
        //要通知鸿蒙端调用setLocation
        window.swift.setCurrentLocation()  //调用鸿蒙端定义的方法
        // setLocation(118.7305,32.1424)
    }

    function setLocation(lon,lat){
        map.setZoomAndCenter(18,[lon,lat])  //设置当前地图的缩放比例和中心位置

        //添加标记点
        //点标记显示内容
        const markerContent = `<div class="custom-content-marker"><img src="http://a.amap.com/jsapi_demos/static/demo-center/icons/dir-via-marker.png"></div>`
        const position = new AMap.LngLat(lon, lat); //Marker 经纬度

        const marker = new AMap.Marker({
          position: position,//[lon,lat],
          content: markerContent, //将 html 传给 content
          offset: new AMap.Pixel(-13, -30), //以 icon 的 [center bottom] 为原点
        });
        map.add(marker) //加到地图中去

        //根据当前位置获取最近的4个地点
        map.plugin(['AMap.PlaceSearch'], function() {
            var PlaceSearchOptions = { //设置PlaceSearch属性
                map: map,
                pageSize: 4,
                pageIndex: 1, //请求页码，默认1
                extensions: "base" //返回信息详略，默认为base（基本信息）
            };
            var MSearch = new AMap.PlaceSearch(PlaceSearchOptions); //构造PlaceSearch类
            AMap.Event.addListener(MSearch, "complete", (result)=>{
                 //返回数组结果
                 //window.swift.alert(JSON.stringify(result))
                 window.swift.receiveData(JSON.stringify(result?.poiList?.pois||[]))
            });
            MSearch.searchNearBy('社区', position); //关键字查询
        });
    }
  </script>

